﻿namespace LightPersist.Transactions
{
	/// <summary>
	/// Represent one atom piece of action
	/// </summary>
	abstract class TheAction
	{
		protected TheAction() {}

		protected TheAction(ModelBase changesOwner, string propertyName)
		{
			TargetModel = changesOwner;
			PropertyName = propertyName;
		}

		public ProcessFlags ProcessFlags { get; set; }

		/// <summary>
		/// The changed object (owner of changed properties of IList)
		/// </summary>
		public ModelBase TargetModel { get; set; }

		/// <summary>
		/// The changed property name (or IList name)
		/// </summary>
		public string PropertyName { get; set; }

		protected abstract string ActTypeToString { get; }

		public override string ToString()
		{
			var pref = string.Format("{0}_{1} ", ActTypeToString, GetHashCode() % 64);
			if (TargetModel != null && PropertyName != null)
			{
				return pref + string.Format("in {0}.{1}", TargetModel, PropertyName.TrimStart('_'));
			}
			return pref;
		}

		public abstract void ProcessDo(TransactionProcessingContext ctx);
		public abstract void ProcessUndo(TransactionProcessingContext ctx);
	}
}